package app.com.business.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;


import app.com.business.bo.AffectationGeneralDepartement;
import app.com.business.bo.Materiel;
import app.com.business.dao.AffectationGeneralDepartementDao;
import app.com.business.dao.MaterielDao;
import app.com.business.exceptions.MaterielException;

import com.groupeA.dao.impl.GenericDaoImpl;

public class MaterielDaoImpl extends GenericDaoImpl<Materiel, Long> implements
		MaterielDao {
	private AffectationGeneralDepartementDao affectationGeneralDao  = new AffectationGeneralDepartementDaoImpl();
	public MaterielDaoImpl() {
		super(Materiel.class);
	}

	public List<Materiel> getMaterielByModel(String model) throws MaterielException{
		try {
		List materiels =  getHibernateTemplate().find("from Materiel where model = ?",
				model);
		if(materiels.size() == 0 | materiels == null | materiels.get(0) == null){
			throw new MaterielException();
		}
		return materiels ;
		}catch(HibernateObjectRetrievalFailureException ex)
		{
			throw new MaterielException();
		}
	}

	public Materiel getMaterielCodeBarres(String codeBarres) throws MaterielException{
		try {
			List materiels =  getHibernateTemplate().find(
					"from Materiel where codeBarres = ?", codeBarres);
			if(materiels.size() == 0 | materiels == null | materiels.get(0) == null){
				throw new MaterielException();
			}
			return (Materiel)materiels.get(0);
		} catch (HibernateObjectRetrievalFailureException e) {
			throw new MaterielException();
		}
		
	}

	public Materiel getMaterielByNumInventaire(String numInventaire) {
		return (Materiel) getHibernateTemplate().find(
				"from Materiel where numInventaire = ?", numInventaire).get(0);
	}

	/*
	 * public List<Materiel> getMaterielByAffectation(Affectation affectation) {
	 * List<Materiel> materielByAffectation = new ArrayList<Materiel>();
	 * List<Materiel> materiels = getHibernateTemplate().find("from Materiel");
	 * for (Materiel materiel : materiels) { /*List<Affectation> affectations =
	 * materiel.getAffectations(); if (affectations != null) { for (Affectation
	 * it : affectations) { if (affectation != null) { if
	 * (affectation.equals(it)) { materielByAffectation.add(materiel); } } } }
	 * if (affectation.equals(materiel.getAffectations())) {
	 * materielByAffectation.add(materiel); } } return materielByAffectation ; }
	 * 
	 * public List<Materiel> getAllByEtat(Etat etat) {
	 * 
	 * List<Materiel> materielByEtat = new ArrayList<Materiel>(); List<Materiel>
	 * materiels = getHibernateTemplate().find("from Materiel"); for (Materiel
	 * materiel : materiels) { /*List<Etat> etats = materiel.getEtats(); if
	 * (etats != null) { for (Etat it : etats) { if (etat != null) { if
	 * (etat.equals(it)) { materielByEtat.add(materiel); } } } } if
	 * (etat.equals(materiel.getEtats())) { materielByEtat.add(materiel); } }
	 * return materielByEtat ; }
	 */
	/*public static void main(String[] args) {
		MaterielDaoImpl mat = new MaterielDaoImpl();
		Materiel m = mat.getMaterielCodeBarres("20092931");
		System.out.println(m);

	}*/

	public void deleteByCodeBarres(String codeBarres) throws MaterielException {
		try {
			Long id = getMaterielCodeBarres(codeBarres).getIdMateriel();
			
			delete(id);
		} catch (HibernateObjectRetrievalFailureException e) {
			throw new MaterielException();
		}

	}

	public List<Materiel> getAllByIdAffectation(Long idAffectationGeneral) throws MaterielException{
		
		try {
			List materiels =  getHibernateTemplate().find("from Materiel where AFFECTATION = ?",idAffectationGeneral);
			if(materiels.size() == 0 | materiels == null | materiels.get(0) == null){
				throw new MaterielException();
			}
			return materiels;

		}catch(HibernateObjectRetrievalFailureException ex){
			throw new MaterielException();
		}
	}

	

}
